<template>
  <div>
    <el-alert
      style="margin-bottom: 15px"
      :title="`数据一共 ${counts} 条`"
      type="info"
      :closable="false"
      show-icon
    >
    </el-alert>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="number" label="试题编号" width="150">
      </el-table-column>
      <el-table-column prop="subject" label="学科" >
      </el-table-column>
      <el-table-column prop="catalog" label="目录" >
      </el-table-column>
      <el-table-column prop="questionType" label="题型" >
        <template slot-scope="scope">
          <div>{{ questionType[scope.row.questionType - 1].label }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="question" label="题干">
        <template slot-scope="scope">
          <div v-html="scope.row.question"></div>
        </template>
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="l80">
        <template slot-scope="scope">
          <div>{{ scope.row.addDate | parseTimeByString }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="difficulty" label="难度" width="167">
        <template slot-scope="scope">
          <div>{{ difficulty[scope.row.difficulty - 1].label }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="creator" label="录入人" width="167">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            :plain="true"
            size="small"
            icon="el-icon-view"
            type="primary"
            title="预览"
            circle
            @click="Preview(scope.row.id)"
          ></el-button>
          <el-button
            :plain="true"
            size="small"
            icon="el-icon-edit"
            type="success"
            title="修改"
            @click="
              $router.push({
                name: 'questions-new',
                params: { id: scope.row.id },
              })
            "
            circle
          ></el-button>
          <el-button
            :plain="true"
            size="small"
            icon="el-icon-delete"
            type="danger"
            title="删除"
            @click="delQuestions(scope.row.id)"
            circle
          >
          </el-button>
          <el-button
            :plain="true"
            size="small"
            icon="el-icon-check"
            type="warning"
            title="加入精选"
            circle
            @click="addChaoice(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px; text-align: right"
      background
      layout="prev, pager, next, sizes, jumper"
      :current-page="data.page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :total="counts"
      :page-size="data.pagesize"
      :page-sizes="[5, 10, 20, 50]"
    >
    </el-pagination>
    <questions-preview
       :dialogVisible="dialogVisible"
      @close="dialogVisible = false"
      :preview="preview"
    ></questions-preview>
  </div>
</template>

<script>
import { difficulty, questionType } from '../../api/hmmm/constants'
import { delQuestions, addChaoice, preview } from '../../api/hmmm/questions'
import QuestionsPreview from './questions-preview'
export default {
  name: 'SubjectList',
  props: {
    tableData: {
      type: Array,
      required: true
    },
    data: {
      type: Object,
      required: true
    },
    counts: {
      type: Number,
      require: true
    }
  },
  data () {
    return {
      difficulty: difficulty,
      questionType: questionType,
      dialogVisible: false,
      preview: {},
    }
  },
  created () {
  },
  mounted () {},
  components: {
    QuestionsPreview
  },
  computed: {},
  watch: {},
  methods: {
    // 基础题库删除
    delQuestions (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            await delQuestions(id)
            this.$parent.subjectList()
          } catch (error) {
            return false
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 加入精选
    addChaoice (id) {
      const h = this.$createElement
      this.$msgbox({
        title: '消息',
        type: 'success',
        message: h('p', null, [
          h('span', null, '此操作将该题目加入精选, 是否继续? '),
          h('i', { style: 'color: teal' })
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async (action) => {
        try {
          await addChaoice(id)
          this.$parent.subjectList()
        } catch (error) {
          return false
        }
        this.$message({
          type: 'success',
          message: '加入成功！'
        })
      })
    },
    // 预览
    async Preview (id) {
      try {
        const { data } = await preview(id)
        this.preview = data
        this.dialogVisible = true
      } catch (error) {
        return false
      }
    },
  dialogVisiblea(tof) {
      this.dialogVisible = tof;
    },
    handleCurrentChange (val) {
      this.data.page = val
      this.$emit('dataChange', this.data)
      this.$parent.subjectList()
    },
    handleSizeChange (val) {
      this.data.pagesize = val
      this.$emit('dataChange', this.data)
      this.$parent.subjectList()
    }
  }
}
</script>

<style scoped>
.question-container .el-pagination__jump {
  margin-left: 0;
}
</style>
